﻿/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Add]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_searchDoanDuong]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_searchDoanDuong]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_searchDoanDuong]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_searchDuong]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_searchDuong]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_searchDuong]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_SearchByOwner]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_SearchByOwner]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_SearchByOwner]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Search]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Search]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Search]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Update]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_GetByMA_THAM_SO]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_GetByMA_THAM_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_GetByMA_THAM_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Delete]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Get]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetCount]    Script Date: 09/28/2011 22:58:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetPaged]    Script Date: 09/28/2011 22:58:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Update]    Script Date: 09/28/2011 22:58:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Add]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Delete]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Get]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_GetCount]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_GetPaged]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Update]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Update]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Add]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Delete]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Get]    Script Date: 09/28/2011 22:58:33 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_GetCount]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_GetPaged]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Restore]    Script Date: 09/28/2011 22:58:34 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Restore]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Restore]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Add]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Delete]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Get]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDD]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDD]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDD]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_getByMaDKS]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_getByMaDKS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_getByMaDKS]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetCount]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetPaged]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Update]    Script Date: 09/28/2011 22:58:32 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Add]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Delete]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Get]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_GetByMA_DKS]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_GetByMA_DKS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_GetByMA_DKS]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_GetCount]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_GetPaged]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Update]    Script Date: 09/28/2011 22:58:31 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Add]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Delete]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Get]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_GetCount]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_GetPaged]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Update]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THAM_SO_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_GetCount]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_GetPaged]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Add]    Script Date: 09/28/2011 22:58:28 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Delete]    Script Date: 09/28/2011 22:58:28 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Get]    Script Date: 09/28/2011 22:58:28 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_GetCount]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_GetPaged]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Update]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Add]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Delete]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Get]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:29 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetCount]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetPaged]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Update]    Script Date: 09/28/2011 22:58:30 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Add]    Script Date: 09/28/2011 22:58:35 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Delete]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Get]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_GetCount]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_GetCount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_GetCount]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_GetPaged]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_GetPaged]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_GetPaged]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Update]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Update]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Update]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Add]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Add]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_Add]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Delete]    Script Date: 09/28/2011 22:58:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Delete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_Delete]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Get]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Get]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_Get]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:37 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc_THUA_DAT_GetByMA_HO_SO]
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[SO_THUA_DAT],
	--[TO_BAN_DO],
	--[CHIEU_DAI],
	--[CHIEU_RONG],
	--[TONG_DIEN_TICH],
	--[DIEN_TICH_CONG_NHAN],
	--[DIEN_TICH_KHONG_CONG_NHAN],
	--[DIEN_TICH_SU_DUNG_CHUNG],
	--[LOAI_HINH_DANG_THUA_DAT],
	--[HINH_DANG_THUA_DAT],
	--[LOAI_DUONG_PHO],
	--[LO_GIOI],
	--[SO_MAT_TIEN],
	--[LOAI_LAN_DUONG],
	--[NOI_DUNG_LAN_DUONG],
	--[LOAI_HEM]

*
FROM
	[THUA_DAT]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Get]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_Get*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_Get]
AS
SELECT
	--[MA_HO_SO],
	--[SO_THUA_DAT],
	--[TO_BAN_DO],
	--[CHIEU_DAI],
	--[CHIEU_RONG],
	--[TONG_DIEN_TICH],
	--[DIEN_TICH_CONG_NHAN],
	--[DIEN_TICH_KHONG_CONG_NHAN],
	--[DIEN_TICH_SU_DUNG_CHUNG],
	--[LOAI_HINH_DANG_THUA_DAT],
	--[HINH_DANG_THUA_DAT],
	--[LOAI_DUONG_PHO],
	--[LO_GIOI],
	--[SO_MAT_TIEN],
	--[LOAI_LAN_DUONG],
	--[NOI_DUNG_LAN_DUONG],
	--[LOAI_HEM]

*
FROM
	[THUA_DAT]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Delete]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_Delete*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[THUA_DAT]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Add]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_Add*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_Add]
	@MA_HO_SO bigint
	,@SO_THUA_DAT NVARCHAR(50)
	,@TO_BAN_DO NVARCHAR(50)
	,@CHIEU_DAI float
	,@CHIEU_RONG float
	,@TONG_DIEN_TICH float
	,@DIEN_TICH_CONG_NHAN float
	,@DIEN_TICH_KHONG_CONG_NHAN float
	,@DIEN_TICH_SU_DUNG_CHUNG float
	,@LOAI_HINH_DANG_THUA_DAT bigint
	,@HINH_DANG_THUA_DAT NVARCHAR(50)
	,@LOAI_DUONG_PHO bigint
	,@LO_GIOI float
	,@SO_MAT_TIEN NVARCHAR(50)
	,@LOAI_LAN_DUONG bigint
	,@NOI_DUNG_LAN_DUONG NVARCHAR(50)
	,@LOAI_HEM bigint

AS

	INSERT INTO [THUA_DAT]
	(
		[MA_HO_SO],
		[SO_THUA_DAT],
		[TO_BAN_DO],
		[CHIEU_DAI],
		[CHIEU_RONG],
		[TONG_DIEN_TICH],
		[DIEN_TICH_CONG_NHAN],
		[DIEN_TICH_KHONG_CONG_NHAN],
		[DIEN_TICH_SU_DUNG_CHUNG],
		[LOAI_HINH_DANG_THUA_DAT],
		[HINH_DANG_THUA_DAT],
		[LOAI_DUONG_PHO],
		[LO_GIOI],
		[SO_MAT_TIEN],
		[LOAI_LAN_DUONG],
		[NOI_DUNG_LAN_DUONG],
		[LOAI_HEM]
	)
	VALUES
	(
		@MA_HO_SO,
		@SO_THUA_DAT,
		@TO_BAN_DO,
		@CHIEU_DAI,
		@CHIEU_RONG,
		@TONG_DIEN_TICH,
		@DIEN_TICH_CONG_NHAN,
		@DIEN_TICH_KHONG_CONG_NHAN,
		@DIEN_TICH_SU_DUNG_CHUNG,
		@LOAI_HINH_DANG_THUA_DAT,
		@HINH_DANG_THUA_DAT,
		@LOAI_DUONG_PHO,
		@LO_GIOI,
		@SO_MAT_TIEN,
		@LOAI_LAN_DUONG,
		@NOI_DUNG_LAN_DUONG,
		@LOAI_HEM
	)
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Update]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_Update*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Update]
	@CACH_TRUNG_TAM float,
	@CACH_DUONG_CHINH float,
	@MA_HO_SO bigint

AS
UPDATE [THONG_TIN_VI_TRI]
SET
	[CACH_TRUNG_TAM] = @CACH_TRUNG_TAM,
	[CACH_DUONG_CHINH] = @CACH_DUONG_CHINH
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_GetPaged]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [THONG_TIN_VI_TRI]


-- query out
SELECT *
FROM [THONG_TIN_VI_TRI]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_GetCount]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_GetCount*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[THONG_TIN_VI_TRI]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[CACH_TRUNG_TAM],
	--[CACH_DUONG_CHINH]

*
FROM
	[THONG_TIN_VI_TRI]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Get]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_Get*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Get]
AS
SELECT
	--[MA_HO_SO],
	--[CACH_TRUNG_TAM],
	--[CACH_DUONG_CHINH]

*
FROM
	[THONG_TIN_VI_TRI]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Delete]    Script Date: 09/28/2011 22:58:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_Delete*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[THONG_TIN_VI_TRI]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THONG_TIN_VI_TRI_Add]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THONG_TIN_VI_TRI_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THONG_TIN_VI_TRI_Add*/
CREATE PROCEDURE [dbo].[sproc_THONG_TIN_VI_TRI_Add]
	@MA_HO_SO bigint
	,@CACH_TRUNG_TAM float
	,@CACH_DUONG_CHINH float

AS

	INSERT INTO [THONG_TIN_VI_TRI]
	(
		[MA_HO_SO],
		[CACH_TRUNG_TAM],
		[CACH_DUONG_CHINH]
	)
	VALUES
	(
		@MA_HO_SO,
		@CACH_TRUNG_TAM,
		@CACH_DUONG_CHINH
	)
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Update]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_Update*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Update]
	@LOAI_NHA bigint,
	@NOI_DUNG_LOAI_NHA NVARCHAR(50),
	@LOAI_CAP_NHA bigint,
	@HUONG_NHA bigint,
	@GIAY_PHEP_XAY_DUNG char(1),
	@DIEN_TICH_XD_CONG_NHAN float,
	@DIEN_TICH_XD_KHONG_CONG_NHAN float,
	@NAM_XAY_DUNG datetime,
	@SO_NAM_SU_DUNG float,
	@DON_GIA_XAY_DUNG float,
	@TUOI_DOI_KINH_TE float,
	@GIA_TRI_CONG_TRINH float,
	@SO_LAN_SUA_CHUA int,
	@MA_HO_SO bigint

AS
UPDATE [CONG_TRINH_XAY_DUNG]
SET
	[LOAI_NHA] = @LOAI_NHA,
	[NOI_DUNG_LOAI_NHA] = @NOI_DUNG_LOAI_NHA,
	[LOAI_CAP_NHA] = @LOAI_CAP_NHA,
	[HUONG_NHA] = @HUONG_NHA,
	[GIAY_PHEP_XAY_DUNG] = @GIAY_PHEP_XAY_DUNG,
	[DIEN_TICH_XD_CONG_NHAN] = @DIEN_TICH_XD_CONG_NHAN,
	[DIEN_TICH_XD_KHONG_CONG_NHAN] = @DIEN_TICH_XD_KHONG_CONG_NHAN,
	[NAM_XAY_DUNG] = @NAM_XAY_DUNG,
	[SO_NAM_SU_DUNG] = @SO_NAM_SU_DUNG,
	[DON_GIA_XAY_DUNG] = @DON_GIA_XAY_DUNG,
	[TUOI_DOI_KINH_TE] = @TUOI_DOI_KINH_TE,
	[GIA_TRI_CONG_TRINH] = @GIA_TRI_CONG_TRINH,
	[SO_LAN_SUA_CHUA] = @SO_LAN_SUA_CHUA
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetPaged]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [CONG_TRINH_XAY_DUNG]


-- query out
SELECT *
FROM [CONG_TRINH_XAY_DUNG]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetCount]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_GetCount*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[CONG_TRINH_XAY_DUNG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[LOAI_NHA],
	--[NOI_DUNG_LOAI_NHA],
	--[LOAI_CAP_NHA],
	--[HUONG_NHA],
	--[GIAY_PHEP_XAY_DUNG],
	--[DIEN_TICH_XD_CONG_NHAN],
	--[DIEN_TICH_XD_KHONG_CONG_NHAN],
	--[NAM_XAY_DUNG],
	--[SO_NAM_SU_DUNG],
	--[DON_GIA_XAY_DUNG],
	--[TUOI_DOI_KINH_TE],
	--[GIA_TRI_CONG_TRINH],
	--[SO_LAN_SUA_CHUA]

*
FROM
	[CONG_TRINH_XAY_DUNG]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Get]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_Get*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Get]
AS
SELECT
	--[MA_HO_SO],
	--[LOAI_NHA],
	--[NOI_DUNG_LOAI_NHA],
	--[LOAI_CAP_NHA],
	--[HUONG_NHA],
	--[GIAY_PHEP_XAY_DUNG],
	--[DIEN_TICH_XD_CONG_NHAN],
	--[DIEN_TICH_XD_KHONG_CONG_NHAN],
	--[NAM_XAY_DUNG],
	--[SO_NAM_SU_DUNG],
	--[DON_GIA_XAY_DUNG],
	--[TUOI_DOI_KINH_TE],
	--[GIA_TRI_CONG_TRINH],
	--[SO_LAN_SUA_CHUA]

*
FROM
	[CONG_TRINH_XAY_DUNG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Delete]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_Delete*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[CONG_TRINH_XAY_DUNG]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CONG_TRINH_XAY_DUNG_Add]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CONG_TRINH_XAY_DUNG_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CONG_TRINH_XAY_DUNG_Add*/
CREATE PROCEDURE [dbo].[sproc_CONG_TRINH_XAY_DUNG_Add]
	@MA_HO_SO bigint
	,@LOAI_NHA bigint
	,@NOI_DUNG_LOAI_NHA NVARCHAR(50)
	,@LOAI_CAP_NHA bigint
	,@HUONG_NHA bigint
	,@GIAY_PHEP_XAY_DUNG char(1)
	,@DIEN_TICH_XD_CONG_NHAN float
	,@DIEN_TICH_XD_KHONG_CONG_NHAN float
	,@NAM_XAY_DUNG datetime
	,@SO_NAM_SU_DUNG float
	,@DON_GIA_XAY_DUNG float
	,@TUOI_DOI_KINH_TE float
	,@GIA_TRI_CONG_TRINH float
	,@SO_LAN_SUA_CHUA int

AS

	INSERT INTO [CONG_TRINH_XAY_DUNG]
	(
		[MA_HO_SO],
		[LOAI_NHA],
		[NOI_DUNG_LOAI_NHA],
		[LOAI_CAP_NHA],
		[HUONG_NHA],
		[GIAY_PHEP_XAY_DUNG],
		[DIEN_TICH_XD_CONG_NHAN],
		[DIEN_TICH_XD_KHONG_CONG_NHAN],
		[NAM_XAY_DUNG],
		[SO_NAM_SU_DUNG],
		[DON_GIA_XAY_DUNG],
		[TUOI_DOI_KINH_TE],
		[GIA_TRI_CONG_TRINH],
		[SO_LAN_SUA_CHUA]
	)
	VALUES
	(
		@MA_HO_SO,
		@LOAI_NHA,
		@NOI_DUNG_LOAI_NHA,
		@LOAI_CAP_NHA,
		@HUONG_NHA,
		@GIAY_PHEP_XAY_DUNG,
		@DIEN_TICH_XD_CONG_NHAN,
		@DIEN_TICH_XD_KHONG_CONG_NHAN,
		@NAM_XAY_DUNG,
		@SO_NAM_SU_DUNG,
		@DON_GIA_XAY_DUNG,
		@TUOI_DOI_KINH_TE,
		@GIA_TRI_CONG_TRINH,
		@SO_LAN_SUA_CHUA
	)
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Update]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_Update*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Update]
	@DONG_HO_DIEN int,
	@CAP_TREO int,
	@CAP_NGAM int,
	@DONG_HO_NUOC int,
	@AP_LUC_NUOC int,
	@CHAT_LUONG_NUOC int,
	@TIENG_ON int,
	@KHOI_BUI int,
	@TINH_TRANG_NGAP int,
	@HE_THONG_CHIEU_SANG int,
	@CHOT_BAO_VE int,
	@AN_NINH int,
	@GAN_CO_SO_GIAO_DUC int,
	@GAN_CO_SO_Y_TE int,
	@GAN_CHO_TRUNG_TAM int,
	@GAN_CO_SO_GIAI_TRI int,
	@GAN_DICH_VU_CONG_CONG int,
	@GAN_THONG_TIN_LIEN_LAC int,
	@MA_HO_SO bigint

AS
UPDATE [CO_SO_HA_TANG]
SET
	[DONG_HO_DIEN] = @DONG_HO_DIEN,
	[CAP_TREO] = @CAP_TREO,
	[CAP_NGAM] = @CAP_NGAM,
	[DONG_HO_NUOC] = @DONG_HO_NUOC,
	[AP_LUC_NUOC] = @AP_LUC_NUOC,
	[CHAT_LUONG_NUOC] = @CHAT_LUONG_NUOC,
	[TIENG_ON] = @TIENG_ON,
	[KHOI_BUI] = @KHOI_BUI,
	[TINH_TRANG_NGAP] = @TINH_TRANG_NGAP,
	[HE_THONG_CHIEU_SANG] = @HE_THONG_CHIEU_SANG,
	[CHOT_BAO_VE] = @CHOT_BAO_VE,
	[AN_NINH] = @AN_NINH,
	[GAN_CO_SO_GIAO_DUC] = @GAN_CO_SO_GIAO_DUC,
	[GAN_CO_SO_Y_TE] = @GAN_CO_SO_Y_TE,
	[GAN_CHO_TRUNG_TAM] = @GAN_CHO_TRUNG_TAM,
	[GAN_CO_SO_GIAI_TRI] = @GAN_CO_SO_GIAI_TRI,
	[GAN_DICH_VU_CONG_CONG] = @GAN_DICH_VU_CONG_CONG,
	[GAN_THONG_TIN_LIEN_LAC] = @GAN_THONG_TIN_LIEN_LAC
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_GetPaged]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [CO_SO_HA_TANG]


-- query out
SELECT *
FROM [CO_SO_HA_TANG]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_GetCount]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_GetCount*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[CO_SO_HA_TANG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[DONG_HO_DIEN],
	--[CAP_TREO],
	--[CAP_NGAM],
	--[DONG_HO_NUOC],
	--[AP_LUC_NUOC],
	--[CHAT_LUONG_NUOC],
	--[TIENG_ON],
	--[KHOI_BUI],
	--[TINH_TRANG_NGAP],
	--[HE_THONG_CHIEU_SANG],
	--[CHOT_BAO_VE],
	--[AN_NINH],
	--[GAN_CO_SO_GIAO_DUC],
	--[GAN_CO_SO_Y_TE],
	--[GAN_CHO_TRUNG_TAM],
	--[GAN_CO_SO_GIAI_TRI],
	--[GAN_DICH_VU_CONG_CONG],
	--[GAN_THONG_TIN_LIEN_LAC]

*
FROM
	[CO_SO_HA_TANG]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Get]    Script Date: 09/28/2011 22:58:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_Get*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Get]
AS
SELECT
	--[MA_HO_SO],
	--[DONG_HO_DIEN],
	--[CAP_TREO],
	--[CAP_NGAM],
	--[DONG_HO_NUOC],
	--[AP_LUC_NUOC],
	--[CHAT_LUONG_NUOC],
	--[TIENG_ON],
	--[KHOI_BUI],
	--[TINH_TRANG_NGAP],
	--[HE_THONG_CHIEU_SANG],
	--[CHOT_BAO_VE],
	--[AN_NINH],
	--[GAN_CO_SO_GIAO_DUC],
	--[GAN_CO_SO_Y_TE],
	--[GAN_CHO_TRUNG_TAM],
	--[GAN_CO_SO_GIAI_TRI],
	--[GAN_DICH_VU_CONG_CONG],
	--[GAN_THONG_TIN_LIEN_LAC]

*
FROM
	[CO_SO_HA_TANG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Delete]    Script Date: 09/28/2011 22:58:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_Delete*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[CO_SO_HA_TANG]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_CO_SO_HA_TANG_Add]    Script Date: 09/28/2011 22:58:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_CO_SO_HA_TANG_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_CO_SO_HA_TANG_Add*/
CREATE PROCEDURE [dbo].[sproc_CO_SO_HA_TANG_Add]
	@MA_HO_SO bigint
	,@DONG_HO_DIEN int
	,@CAP_TREO int
	,@CAP_NGAM int
	,@DONG_HO_NUOC int
	,@AP_LUC_NUOC int
	,@CHAT_LUONG_NUOC int
	,@TIENG_ON int
	,@KHOI_BUI int
	,@TINH_TRANG_NGAP int
	,@HE_THONG_CHIEU_SANG int
	,@CHOT_BAO_VE int
	,@AN_NINH int
	,@GAN_CO_SO_GIAO_DUC int
	,@GAN_CO_SO_Y_TE int
	,@GAN_CHO_TRUNG_TAM int
	,@GAN_CO_SO_GIAI_TRI int
	,@GAN_DICH_VU_CONG_CONG int
	,@GAN_THONG_TIN_LIEN_LAC int

AS

	INSERT INTO [CO_SO_HA_TANG]
	(
		[MA_HO_SO],
		[DONG_HO_DIEN],
		[CAP_TREO],
		[CAP_NGAM],
		[DONG_HO_NUOC],
		[AP_LUC_NUOC],
		[CHAT_LUONG_NUOC],
		[TIENG_ON],
		[KHOI_BUI],
		[TINH_TRANG_NGAP],
		[HE_THONG_CHIEU_SANG],
		[CHOT_BAO_VE],
		[AN_NINH],
		[GAN_CO_SO_GIAO_DUC],
		[GAN_CO_SO_Y_TE],
		[GAN_CHO_TRUNG_TAM],
		[GAN_CO_SO_GIAI_TRI],
		[GAN_DICH_VU_CONG_CONG],
		[GAN_THONG_TIN_LIEN_LAC]
	)
	VALUES
	(
		@MA_HO_SO,
		@DONG_HO_DIEN,
		@CAP_TREO,
		@CAP_NGAM,
		@DONG_HO_NUOC,
		@AP_LUC_NUOC,
		@CHAT_LUONG_NUOC,
		@TIENG_ON,
		@KHOI_BUI,
		@TINH_TRANG_NGAP,
		@HE_THONG_CHIEU_SANG,
		@CHOT_BAO_VE,
		@AN_NINH,
		@GAN_CO_SO_GIAO_DUC,
		@GAN_CO_SO_Y_TE,
		@GAN_CHO_TRUNG_TAM,
		@GAN_CO_SO_GIAI_TRI,
		@GAN_DICH_VU_CONG_CONG,
		@GAN_THONG_TIN_LIEN_LAC
	)
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_GetPaged]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [THUA_DAT]


-- query out
SELECT *
FROM [THUA_DAT]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_GetCount]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_GetCount*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[THUA_DAT]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Update]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_Update*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_Update]
	@PARENT_ID bigint,
	@LOAI_THAM_SO NVARCHAR(50),
	@NOI_DUNG NVARCHAR(255),
	@SEQUENCE int,
	@GHI_CHU NVARCHAR(50),
	@MA_THAM_SO bigint

AS
UPDATE [THAM_SO]
SET
	[PARENT_ID] = @PARENT_ID,
	[LOAI_THAM_SO] = @LOAI_THAM_SO,
	[NOI_DUNG] = @NOI_DUNG,
	[SEQUENCE] = @SEQUENCE,
	[GHI_CHU] = @GHI_CHU
WHERE
	[MA_THAM_SO] = @MA_THAM_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_GetPaged]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_THAM_SO]
FROM [THAM_SO]


-- query out
SELECT *
FROM [THAM_SO]
WHERE [MA_THAM_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_GetCount]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_GetCount*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[THAM_SO]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Get]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_Get*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_Get]
AS
SELECT
	--[MA_THAM_SO],
	--[PARENT_ID],
	--[LOAI_THAM_SO],
	--[NOI_DUNG],
	--[SEQUENCE],
	--[GHI_CHU]

*
FROM
	[THAM_SO]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Delete]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_Delete*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_Delete]
	@MA_THAM_SO bigint
AS
DELETE
FROM
	[THAM_SO]
WHERE
	[MA_THAM_SO] = @MA_THAM_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_Add]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_Add*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_Add]
	@MA_THAM_SO bigint OUTPUT
	,@PARENT_ID bigint
	,@LOAI_THAM_SO NVARCHAR(50)
	,@NOI_DUNG NVARCHAR(255)
	,@SEQUENCE int
	,@GHI_CHU NVARCHAR(50)

AS

	INSERT INTO [THAM_SO]
	(
		[PARENT_ID],
		[LOAI_THAM_SO],
		[NOI_DUNG],
		[SEQUENCE],
		[GHI_CHU]
	)
	VALUES
	(
		@PARENT_ID,
		@LOAI_THAM_SO,
		@NOI_DUNG,
		@SEQUENCE,
		@GHI_CHU
	)
	SELECT
		@MA_THAM_SO = @@Identity

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Update]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_Update*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Update]
	@QUAN bigint,
	@PHUONG bigint,
	@DUONG NVARCHAR(50),
	@DOAN_DUONG NVARCHAR(50),
	@NGAY_KHOI_TAO datetime,
	@ACTIVE_FLAG char(1),
	@MA_DKS bigint

AS
UPDATE [DUONG_KHAO_SAT]
SET
	[QUAN] = @QUAN,
	[PHUONG] = @PHUONG,
	[DUONG] = @DUONG,
	[DOAN_DUONG] = @DOAN_DUONG,
	[NGAY_KHOI_TAO] = @NGAY_KHOI_TAO,
	[ACTIVE_FLAG] = @ACTIVE_FLAG
WHERE
	[MA_DKS] = @MA_DKS
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_GetPaged]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_DKS]
FROM [DUONG_KHAO_SAT]


-- query out
SELECT *
FROM [DUONG_KHAO_SAT]
WHERE [MA_DKS]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_GetCount]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_GetCount*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[DUONG_KHAO_SAT]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_GetByMA_DKS]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_GetByMA_DKS]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_GetByMA_DKS*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_GetByMA_DKS]
@MA_DKS bigint
AS
SELECT
	--[MA_DKS],
	--[QUAN],
	--[PHUONG],
	--[DUONG],
	--[DOAN_DUONG],
	--[NGAY_KHOI_TAO],
	--[ACTIVE_FLAG]

*
FROM
	[DUONG_KHAO_SAT]
WHERE
	[MA_DKS] = @MA_DKS
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Get]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_Get*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Get]
AS
SELECT
dks.*
FROM
	[DUONG_KHAO_SAT] dks
	INNER JOIN HO_SO_THAM_DINH hstd ON hstd.MA_DKS = dks.MA_DKS
WHERE 
	hstd.ACTIVE_FLAG = ''Y''
GROUP BY dks.MA_DKS, dks.QUAN, dks.PHUONG, dks.DUONG, dks.DOAN_DUONG,
      dks.NGAY_KHOI_TAO, dks.ACTIVE_FLAG
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Delete]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_Delete*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Delete]
	@MA_DKS bigint
AS
DELETE
FROM
	[DUONG_KHAO_SAT]
WHERE
	[MA_DKS] = @MA_DKS
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_Add]    Script Date: 09/28/2011 22:58:30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_DUONG_KHAO_SAT_Add*/
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_Add]
	@MA_DKS bigint OUTPUT
	,@QUAN bigint
	,@PHUONG bigint
	,@DUONG NVARCHAR(50)
	,@DOAN_DUONG NVARCHAR(50)
	,@NGAY_KHOI_TAO datetime
	,@ACTIVE_FLAG char(1)

AS
	SELECT @MA_DKS=dks.MA_DKS
	  FROM DUONG_KHAO_SAT dks WHERE dks.QUAN=@QUAN AND dks.PHUONG=@PHUONG
		AND dks.DUONG=@DUONG AND dks.DOAN_DUONG=@DOAN_DUONG
	IF (@MA_DKS>0)
	BEGIN
		SELECT @MA_DKS
	END
	ELSE
	BEGIN
		INSERT INTO [DUONG_KHAO_SAT]
		(
			[QUAN],
			[PHUONG],
			[DUONG],
			[DOAN_DUONG],
			[NGAY_KHOI_TAO],
			[ACTIVE_FLAG]
		)
		VALUES
		(
			@QUAN,
			@PHUONG,
			@DUONG,
			@DOAN_DUONG,
			@NGAY_KHOI_TAO,
			@ACTIVE_FLAG
		)
		SELECT
			@MA_DKS = @@Identity
	END
	

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Update]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_Update*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Update]
	@MA_DKS bigint,
	@GIA_DAT float,
	@NGUOI_KHOI_TAO uniqueidentifier,
	@NGAY_KHOI_TAO datetime,
	@NGAY_CAP_NHAT datetime,
	@ACTIVE_FLAG char(1),
	@MA_GDD bigint

AS
UPDATE [GIA_DOAN_DUONG]
SET
	[MA_DKS] = @MA_DKS,
	[GIA_DAT] = @GIA_DAT,
	[NGUOI_KHOI_TAO] = @NGUOI_KHOI_TAO,
	[NGAY_KHOI_TAO] = @NGAY_KHOI_TAO,
	[NGAY_CAP_NHAT] = @NGAY_CAP_NHAT,
	[ACTIVE_FLAG] = @ACTIVE_FLAG
WHERE
	[MA_GDD] = @MA_GDD
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetPaged]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_GDD]
FROM [GIA_DOAN_DUONG]


-- query out
SELECT *
FROM [GIA_DOAN_DUONG]
WHERE [MA_GDD]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetCount]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_GetCount*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[GIA_DOAN_DUONG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_getByMaDKS]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_getByMaDKS]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_getByMaDKS*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_getByMaDKS]
	@MA_DKS bigint
AS
	SELECT
		gdd.MA_GDD,
		gdd.MA_DKS,
		gdd.GIA_DAT,
		gdd.NGUOI_KHOI_TAO,
		gdd.NGAY_KHOI_TAO,
		gdd.NGAY_CAP_NHAT,
		gdd.ACTIVE_FLAG
	FROM
		GIA_DOAN_DUONG gdd
	WHERE gdd.MA_DKS = @MA_DKS AND gdd.ACTIVE_FLAG = ''Y'' AND 
		gdd.NGAY_CAP_NHAT = (SELECT max(gdd1.NGAY_CAP_NHAT)
		                       FROM GIA_DOAN_DUONG gdd1 WHERE gdd1.MA_GDD = gdd.MA_GDD)
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged]
	@RecPerPage INT,
	@PageIndex INT,
	@MA_GDD bigint
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_GDD]
FROM [GIA_DOAN_DUONG]
WHERE [MA_GDD] = @MA_GDD


-- query out
SELECT *
FROM [GIA_DOAN_DUONG]
WHERE [MA_GDD] = @MA_GDD
AND [MA_GDD]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDD]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDD]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_GetByMA_GDD*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_GetByMA_GDD]
@MA_GDD		bigint
AS
SELECT
	[MA_GDD],
	[MA_DKS],
	[GIA_DAT],
	[NGUOI_KHOI_TAO],
	[NGAY_KHOI_TAO],
	[NGAY_CAP_NHAT],
	[ACTIVE_FLAG]
FROM
	[GIA_DOAN_DUONG]
WHERE
	[MA_GDD] = @MA_GDD
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Get]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_Get*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Get]
AS
SELECT
	--[MA_GDD],
	--[MA_DKS],
	--[GIA_DAT],
	--[NGUOI_KHOI_TAO],
	--[NGAY_KHOI_TAO],
	--[NGAY_CAP_NHAT],
	--[ACTIVE_FLAG]

*
FROM
	[GIA_DOAN_DUONG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Delete]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'/* Procedure sproc_GIA_DOAN_DUONG_Delete*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Delete]
	@MA_GDD bigint
AS
DELETE
FROM
	[GIA_DOAN_DUONG]
WHERE
	[MA_GDD] = @MA_GDD
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_DOAN_DUONG_Add]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_DOAN_DUONG_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_DOAN_DUONG_Add*/
CREATE PROCEDURE [dbo].[sproc_GIA_DOAN_DUONG_Add]
	@MA_GDD bigint OUTPUT
	,@MA_DKS bigint
	,@GIA_DAT float
	,@NGUOI_KHOI_TAO uniqueidentifier
	,@NGAY_KHOI_TAO datetime
	,@NGAY_CAP_NHAT datetime
	,@ACTIVE_FLAG char(1)

AS
BEGIN
	IF EXISTS (SELECT gdd.MA_DKS FROM GIA_DOAN_DUONG gdd WHERE gdd.MA_DKS=@MA_DKS)
	BEGIN
		UPDATE GIA_DOAN_DUONG
		SET
			GIA_DAT = @GIA_DAT,
			NGAY_KHOI_TAO = @NGAY_KHOI_TAO,
			NGAY_CAP_NHAT = @NGAY_CAP_NHAT
		WHERE MA_DKS=@MA_DKS
		SELECT @MA_GDD=gdd.MA_GDD
		  FROM GIA_DOAN_DUONG gdd WHERE gdd.MA_DKS=@MA_DKS
	END
	ELSE
	BEGIN
		INSERT INTO [GIA_DOAN_DUONG]
		(
			[MA_DKS],
			[GIA_DAT],
			[NGUOI_KHOI_TAO],
			[NGAY_KHOI_TAO],
			[NGAY_CAP_NHAT],
			[ACTIVE_FLAG]
		)
		VALUES
		(
			@MA_DKS,
			@GIA_DAT,
			@NGUOI_KHOI_TAO,
			@NGAY_KHOI_TAO,
			@NGAY_CAP_NHAT,
			@ACTIVE_FLAG
		)
		SELECT
			@MA_GDD = @@Identity
	END
	
	/*
	<root>
		<item value="" date="" />
	</root>
	 */
	IF ISNULL(@MA_GDD,0) != 0
	BEGIN
		DECLARE @History XML
		SET @History=(SELECT HISTORY FROM GIA_DOAN_DUONG gdd WHERE gdd.MA_GDD=@MA_GDD)
		IF cast(ISNULL(@History,'') AS VARCHAR(MAX)) = '' 
		BEGIN
			-- set default value
			SET @History=N'<root/>'
		END 
		DECLARE @Node XML,@Return XML
		set @Node = ''<item value="''+cast(@GIA_DAT AS NVARCHAR(20))+''" date="''+CONVERT(VARCHAR(8),GETDATE(),112)+''" />''
		set @Return = convert(xml, (convert(nvarchar(max), @History) + convert(nvarchar(max), @Node)))
		set @Return.modify(''insert /*[2] as first into /*[1]'')
		set @Return.modify(''delete /*[2]'')
		
		UPDATE GIA_DOAN_DUONG
		SET
			HISTORY = @Return
		WHERE MA_GDD=@MA_GDD
	END
END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Restore]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Restore]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N' 
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Restore]
	@MA_HO_SO BIGINT
AS
BEGIN
	UPDATE HO_SO_THAM_DINH
	SET
		ACTIVE_FLAG = ''T''
	WHERE MA_HO_SO = @MA_HO_SO
	
	SELECT 1
END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_GetPaged]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [HO_SO_THAM_DINH]


-- query out
SELECT *
FROM [HO_SO_THAM_DINH]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_GetCount]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_GetCount*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[HO_SO_THAM_DINH]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[MA_DKS],
	--[SO_PHIEU],
	--[TEN_CHU_SU_DUNG],
	--[DIA_CHI],
	--[GPS_LATITUDE],
	--[GPS_LONGTITUDE],
	--[GPS_CHI_CHU],
	--[TINH_TRANG_PHAP_LY],
	--[NOI_DUNG_PHAP_LY],
	--[LOAI_KHU_VUC_KINH_DOANH],
	--[Y_KIEN_THAM_DINH],
	--[GIA_DAT_1],
	--[GIA_DAT_2],
	--[GIA_DAT_BINH_QUAN_1],
	--[GIA_DAT_BINH_QUAN_2],
	--[NGUOI_KHOI_TAO],
	--[NGAY_KHOI_TAO],
	--[NGAY_CAP_NHAT],
	--[ACTIVE_FLAG]

*
FROM
	[HO_SO_THAM_DINH]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Get]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_Get*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Get]
AS
SELECT
	--[MA_HO_SO],
	--[MA_DKS],
	--[SO_PHIEU],
	--[TEN_CHU_SU_DUNG],
	--[DIA_CHI],
	--[GPS_LATITUDE],
	--[GPS_LONGTITUDE],
	--[GPS_CHI_CHU],
	--[TINH_TRANG_PHAP_LY],
	--[NOI_DUNG_PHAP_LY],
	--[LOAI_KHU_VUC_KINH_DOANH],
	--[Y_KIEN_THAM_DINH],
	--[GIA_DAT_1],
	--[GIA_DAT_2],
	--[GIA_DAT_BINH_QUAN_1],
	--[GIA_DAT_BINH_QUAN_2],
	--[NGUOI_KHOI_TAO],
	--[NGAY_KHOI_TAO],
	--[NGAY_CAP_NHAT],
	--[ACTIVE_FLAG]

*
FROM
	[HO_SO_THAM_DINH]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Delete]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_Delete*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[HO_SO_THAM_DINH]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Add]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_Add*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Add]
	@MA_HO_SO bigint OUTPUT
	,@MA_DKS bigint
	,@SO_PHIEU NVARCHAR(255)
	,@TEN_CHU_SU_DUNG NVARCHAR(255)
	,@DIA_CHI NVARCHAR(255)
	,@GPS_LATITUDE float
	,@GPS_LONGTITUDE float
	,@GPS_CHI_CHU NVARCHAR(255)
	,@TINH_TRANG_PHAP_LY bigint
	,@NOI_DUNG_PHAP_LY NVARCHAR(255)
	,@LOAI_KHU_VUC_KINH_DOANH int
	,@Y_KIEN_THAM_DINH NVARCHAR(1023)
	,@GIA_DAT_1 float
	,@GIA_DAT_2 float
	,@GIA_DAT_BINH_QUAN_1 float
	,@GIA_DAT_BINH_QUAN_2 float
	,@NGUOI_KHOI_TAO uniqueidentifier
	,@NGAY_KHOI_TAO datetime
	,@NGAY_CAP_NHAT datetime
	,@ACTIVE_FLAG char(1)

AS

	INSERT INTO [HO_SO_THAM_DINH]
	(
		[MA_DKS],
		[SO_PHIEU],
		[TEN_CHU_SU_DUNG],
		[DIA_CHI],
		[GPS_LATITUDE],
		[GPS_LONGTITUDE],
		[GPS_CHI_CHU],
		[TINH_TRANG_PHAP_LY],
		[NOI_DUNG_PHAP_LY],
		[LOAI_KHU_VUC_KINH_DOANH],
		[Y_KIEN_THAM_DINH],
		[GIA_DAT_1],
		[GIA_DAT_2],
		[GIA_DAT_BINH_QUAN_1],
		[GIA_DAT_BINH_QUAN_2],
		[NGUOI_CAP_NHAT],
		[NGUOI_KHOI_TAO],
		[NGAY_KHOI_TAO],
		[NGAY_CAP_NHAT],
		[ACTIVE_FLAG]
	)
	VALUES
	(
		@MA_DKS,
		@SO_PHIEU,
		@TEN_CHU_SU_DUNG,
		@DIA_CHI,
		@GPS_LATITUDE,
		@GPS_LONGTITUDE,
		@GPS_CHI_CHU,
		@TINH_TRANG_PHAP_LY,
		@NOI_DUNG_PHAP_LY,
		@LOAI_KHU_VUC_KINH_DOANH,
		@Y_KIEN_THAM_DINH,
		@GIA_DAT_1,
		@GIA_DAT_2,
		@GIA_DAT_BINH_QUAN_1,
		@GIA_DAT_BINH_QUAN_2,
		@NGUOI_KHOI_TAO,
		@NGUOI_KHOI_TAO,
		@NGAY_KHOI_TAO,
		@NGAY_CAP_NHAT,
		@ACTIVE_FLAG
	)
	SELECT
		@MA_HO_SO = @@Identity

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Update]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_HO_SO_THAM_DINH_Update*/
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Update]
	@MA_DKS bigint,
	@SO_PHIEU NVARCHAR(255),
	@TEN_CHU_SU_DUNG NVARCHAR(255),
	@DIA_CHI NVARCHAR(255),
	@GPS_LATITUDE float,
	@GPS_LONGTITUDE float,
	@GPS_CHI_CHU NVARCHAR(255),
	@TINH_TRANG_PHAP_LY bigint,
	@NOI_DUNG_PHAP_LY NVARCHAR(255),
	@LOAI_KHU_VUC_KINH_DOANH int,
	@Y_KIEN_THAM_DINH NVARCHAR(1023),
	@GIA_DAT_1 float,
	@GIA_DAT_2 float,
	@GIA_DAT_BINH_QUAN_1 float,
	@GIA_DAT_BINH_QUAN_2 float,
	@NGUOI_KHOI_TAO uniqueidentifier,
	@NGAY_KHOI_TAO datetime,
	@NGAY_CAP_NHAT datetime,
	@ACTIVE_FLAG char(1),
	@MA_HO_SO bigint

AS
UPDATE [HO_SO_THAM_DINH]
SET
	[MA_DKS] = @MA_DKS,
	[SO_PHIEU] = @SO_PHIEU,
	[TEN_CHU_SU_DUNG] = @TEN_CHU_SU_DUNG,
	[DIA_CHI] = @DIA_CHI,
	[GPS_LATITUDE] = @GPS_LATITUDE,
	[GPS_LONGTITUDE] = @GPS_LONGTITUDE,
	[GPS_CHI_CHU] = @GPS_CHI_CHU,
	[TINH_TRANG_PHAP_LY] = @TINH_TRANG_PHAP_LY,
	[NOI_DUNG_PHAP_LY] = @NOI_DUNG_PHAP_LY,
	[LOAI_KHU_VUC_KINH_DOANH] = @LOAI_KHU_VUC_KINH_DOANH,
	[Y_KIEN_THAM_DINH] = @Y_KIEN_THAM_DINH,
	[GIA_DAT_1] = @GIA_DAT_1,
	[GIA_DAT_2] = @GIA_DAT_2,
	[GIA_DAT_BINH_QUAN_1] = @GIA_DAT_BINH_QUAN_1,
	[GIA_DAT_BINH_QUAN_2] = @GIA_DAT_BINH_QUAN_2,
	[NGUOI_CAP_NHAT] = @NGUOI_KHOI_TAO,
	[NGAY_KHOI_TAO] = @NGAY_KHOI_TAO,
	[NGAY_CAP_NHAT] = @NGAY_CAP_NHAT,
	[ACTIVE_FLAG] = @ACTIVE_FLAG
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Update]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_Update*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Update]
	@GIA_GIAO_DICH_TRIEU_DONG float,
	@GIA_GIAO_DICH_CAY_VANG float,
	@THOI_DIEM_GIAO_DICH datetime,
	@LOAI_THANH_TOAN bigint,
	@NOI_DUNG_THANH_TOAN NVARCHAR(50),
	@MA_HO_SO bigint

AS
UPDATE [GIA_THAM_DINH]
SET
	[GIA_GIAO_DICH_TRIEU_DONG] = @GIA_GIAO_DICH_TRIEU_DONG,
	[GIA_GIAO_DICH_CAY_VANG] = @GIA_GIAO_DICH_CAY_VANG,
	[THOI_DIEM_GIAO_DICH] = @THOI_DIEM_GIAO_DICH,
	[LOAI_THANH_TOAN] = @LOAI_THANH_TOAN,
	[NOI_DUNG_THANH_TOAN] = @NOI_DUNG_THANH_TOAN
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_GetPaged]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [GIA_THAM_DINH]


-- query out
SELECT *
FROM [GIA_THAM_DINH]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_GetCount]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_GetCount*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[GIA_THAM_DINH]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[GIA_GIAO_DICH_TRIEU_DONG],
	--[GIA_GIAO_DICH_CAY_VANG],
	--[THOI_DIEM_GIAO_DICH],
	--[LOAI_THANH_TOAN],
	--[NOI_DUNG_THANH_TOAN]

*
FROM
	[GIA_THAM_DINH]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Get]    Script Date: 09/28/2011 22:58:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_Get*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Get]
AS
SELECT
	--[MA_HO_SO],
	--[GIA_GIAO_DICH_TRIEU_DONG],
	--[GIA_GIAO_DICH_CAY_VANG],
	--[THOI_DIEM_GIAO_DICH],
	--[LOAI_THANH_TOAN],
	--[NOI_DUNG_THANH_TOAN]

*
FROM
	[GIA_THAM_DINH]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Delete]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_Delete*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[GIA_THAM_DINH]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_GIA_THAM_DINH_Add]    Script Date: 09/28/2011 22:58:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GIA_THAM_DINH_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_GIA_THAM_DINH_Add*/
CREATE PROCEDURE [dbo].[sproc_GIA_THAM_DINH_Add]
	@MA_HO_SO bigint
	,@GIA_GIAO_DICH_TRIEU_DONG float
	,@GIA_GIAO_DICH_CAY_VANG float
	,@THOI_DIEM_GIAO_DICH datetime
	,@LOAI_THANH_TOAN bigint
	,@NOI_DUNG_THANH_TOAN NVARCHAR(50)

AS

	INSERT INTO [GIA_THAM_DINH]
	(
		[MA_HO_SO],
		[GIA_GIAO_DICH_TRIEU_DONG],
		[GIA_GIAO_DICH_CAY_VANG],
		[THOI_DIEM_GIAO_DICH],
		[LOAI_THANH_TOAN],
		[NOI_DUNG_THANH_TOAN]
	)
	VALUES
	(
		@MA_HO_SO,
		@GIA_GIAO_DICH_TRIEU_DONG,
		@GIA_GIAO_DICH_CAY_VANG,
		@THOI_DIEM_GIAO_DICH,
		@LOAI_THANH_TOAN,
		@NOI_DUNG_THANH_TOAN
	)
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Update]    Script Date: 09/28/2011 22:58:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_VAT_LIEU_XAY_DUNG_Update*/
CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Update]
	@LOAI_KET_CAU_KHUNG bigint,
	@NOI_DUNG_KET_CAU_KHUNG NVARCHAR(50),
	@LOAI_KET_CAU_MONG bigint,
	@NOI_DUNG_KET_CAU_MONG NVARCHAR(50),
	@LOAI_KET_CAU_TUONG_NHA bigint,
	@NOI_DUNG_KET_CAU_TUONG_NHA NVARCHAR(50),
	@LOAI_KET_CAU_NEN bigint,
	@NOI_DUNG_KET_CAU_NEN NVARCHAR(50),
	@LOAI_KET_CAU_MAI bigint,
	@NOI_DUNG_KET_CAU_MAI NVARCHAR(50),
	@CHAT_LUONG_MONG float,
	@CHAT_LUONG_SAN float,
	@CHAT_LUONG_TUONG float,
	@CHAT_LUONG_TRAN float,
	@CHAT_LUONG_MAI float,
	@TI_LE_KET_CAU_MONG float,
	@TI_LE_KET_CAU_SAN float,
	@TI_LE_KET_CAU_TUONG float,
	@TI_LE_KET_CAU_TRAN float,
	@TI_LE_KET_CAU_MAI float,
	@SO_PHONG_NGU int,
	@SO_NHA_VE_SINH int,
	@LOAI_TRANG_TRI_NOI_THAT bigint,
	@NOI_DUNG_TRANG_TRI_NOI_THAT NVARCHAR(50),
	@LOAI_CONG_TRINH_XAY_DUNG bigint,
	@NOI_DUNG_CONG_TRINH_XAY_DUNG NVARCHAR(50),
	@MA_HO_SO bigint

AS
UPDATE [VAT_LIEU_XAY_DUNG]
SET
	[LOAI_KET_CAU_KHUNG] = @LOAI_KET_CAU_KHUNG,
	[NOI_DUNG_KET_CAU_KHUNG] = @NOI_DUNG_KET_CAU_KHUNG,
	[LOAI_KET_CAU_MONG] = @LOAI_KET_CAU_MONG,
	[NOI_DUNG_KET_CAU_MONG] = @NOI_DUNG_KET_CAU_MONG,
	[LOAI_KET_CAU_TUONG_NHA] = @LOAI_KET_CAU_TUONG_NHA,
	[NOI_DUNG_KET_CAU_TUONG_NHA] = @NOI_DUNG_KET_CAU_TUONG_NHA,
	[LOAI_KET_CAU_NEN] = @LOAI_KET_CAU_NEN,
	[NOI_DUNG_KET_CAU_NEN] = @NOI_DUNG_KET_CAU_NEN,
	[LOAI_KET_CAU_MAI] = @LOAI_KET_CAU_MAI,
	[NOI_DUNG_KET_CAU_MAI] = @NOI_DUNG_KET_CAU_MAI,
	[CHAT_LUONG_MONG] = @CHAT_LUONG_MONG,
	[CHAT_LUONG_SAN] = @CHAT_LUONG_SAN,
	[CHAT_LUONG_TUONG] = @CHAT_LUONG_TUONG,
	[CHAT_LUONG_TRAN] = @CHAT_LUONG_TRAN,
	[CHAT_LUONG_MAI] = @CHAT_LUONG_MAI,
	[TI_LE_KET_CAU_MONG] = @TI_LE_KET_CAU_MONG,
	[TI_LE_KET_CAU_SAN] = @TI_LE_KET_CAU_SAN,
	[TI_LE_KET_CAU_TUONG] = @TI_LE_KET_CAU_TUONG,
	[TI_LE_KET_CAU_TRAN] = @TI_LE_KET_CAU_TRAN,
	[TI_LE_KET_CAU_MAI] = @TI_LE_KET_CAU_MAI,
	[SO_PHONG_NGU] = @SO_PHONG_NGU,
	[SO_NHA_VE_SINH] = @SO_NHA_VE_SINH,
	[LOAI_TRANG_TRI_NOI_THAT] = @LOAI_TRANG_TRI_NOI_THAT,
	[NOI_DUNG_TRANG_TRI_NOI_THAT] = @NOI_DUNG_TRANG_TRI_NOI_THAT,
	[LOAI_CONG_TRINH_XAY_DUNG] = @LOAI_CONG_TRINH_XAY_DUNG,
	[NOI_DUNG_CONG_TRINH_XAY_DUNG] = @NOI_DUNG_CONG_TRINH_XAY_DUNG
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetPaged]    Script Date: 09/28/2011 22:58:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_GetPaged]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_VAT_LIEU_XAY_DUNG_GetPaged*/
CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetPaged]
	@RecPerPage INT,
	@PageIndex INT
AS

DECLARE @FirstRec INT
DECLARE @LastRec INT

SET @FirstRec = (@PageIndex - 1)*@RecPerPage + 1
SET @LastRec = @PageIndex *@RecPerPage 

-- create temp table to paging
CREATE TABLE #tmp_paging_index
(
	recID		INT IDENTITY(1,1) NOT NULL,
	messageID	bigint
)

-- insert temp records
INSERT INTO #tmp_paging_index(messageID)
SELECT [MA_HO_SO]
FROM [VAT_LIEU_XAY_DUNG]


-- query out
SELECT *
FROM [VAT_LIEU_XAY_DUNG]
WHERE [MA_HO_SO]
IN (
	SELECT messageID 
	FROM #tmp_paging_index 
	WHERE recID >= @FirstRec AND recID <= @LastRec
)
DROP TABLE #tmp_paging_index

' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetCount]    Script Date: 09/28/2011 22:58:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_GetCount]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_VAT_LIEU_XAY_DUNG_GetCount*/
CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetCount]
AS
SELECT
	COUNT(*)
FROM
	[VAT_LIEU_XAY_DUNG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO]    Script Date: 09/28/2011 22:58:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO*/
CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_GetByMA_HO_SO]
@MA_HO_SO bigint
AS
SELECT
	--[MA_HO_SO],
	--[LOAI_KET_CAU_KHUNG],
	--[NOI_DUNG_KET_CAU_KHUNG],
	--[LOAI_KET_CAU_MONG],
	--[NOI_DUNG_KET_CAU_MONG],
	--[LOAI_KET_CAU_TUONG_NHA],
	--[NOI_DUNG_KET_CAU_TUONG_NHA],
	--[LOAI_KET_CAU_NEN],
	--[NOI_DUNG_KET_CAU_NEN],
	--[LOAI_KET_CAU_MAI],
	--[NOI_DUNG_KET_CAU_MAI],
	--[CHAT_LUONG_MONG],
	--[CHAT_LUONG_SAN],
	--[CHAT_LUONG_TUONG],
	--[CHAT_LUONG_TRAN],
	--[CHAT_LUONG_MAI],
	--[TI_LE_KET_CAU_MONG],
	--[TI_LE_KET_CAU_SAN],
	--[TI_LE_KET_CAU_TUONG],
	--[TI_LE_KET_CAU_TRAN],
	--[TI_LE_KET_CAU_MAI],
	--[SO_PHONG_NGU],
	--[SO_NHA_VE_SINH],
	--[LOAI_TRANG_TRI_NOI_THAT],
	--[NOI_DUNG_TRANG_TRI_NOI_THAT],
	--[LOAI_CONG_TRINH_XAY_DUNG],
	--[NOI_DUNG_CONG_TRINH_XAY_DUNG]

*
FROM
	[VAT_LIEU_XAY_DUNG]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Get]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Get]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_VAT_LIEU_XAY_DUNG_Get*/
CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Get]
AS
SELECT
	--[MA_HO_SO],
	--[LOAI_KET_CAU_KHUNG],
	--[NOI_DUNG_KET_CAU_KHUNG],
	--[LOAI_KET_CAU_MONG],
	--[NOI_DUNG_KET_CAU_MONG],
	--[LOAI_KET_CAU_TUONG_NHA],
	--[NOI_DUNG_KET_CAU_TUONG_NHA],
	--[LOAI_KET_CAU_NEN],
	--[NOI_DUNG_KET_CAU_NEN],
	--[LOAI_KET_CAU_MAI],
	--[NOI_DUNG_KET_CAU_MAI],
	--[CHAT_LUONG_MONG],
	--[CHAT_LUONG_SAN],
	--[CHAT_LUONG_TUONG],
	--[CHAT_LUONG_TRAN],
	--[CHAT_LUONG_MAI],
	--[TI_LE_KET_CAU_MONG],
	--[TI_LE_KET_CAU_SAN],
	--[TI_LE_KET_CAU_TUONG],
	--[TI_LE_KET_CAU_TRAN],
	--[TI_LE_KET_CAU_MAI],
	--[SO_PHONG_NGU],
	--[SO_NHA_VE_SINH],
	--[LOAI_TRANG_TRI_NOI_THAT],
	--[NOI_DUNG_TRANG_TRI_NOI_THAT],
	--[LOAI_CONG_TRINH_XAY_DUNG],
	--[NOI_DUNG_CONG_TRINH_XAY_DUNG]

*
FROM
	[VAT_LIEU_XAY_DUNG]
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Delete]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Delete]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_VAT_LIEU_XAY_DUNG_Delete*/
CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Delete]
	@MA_HO_SO bigint
AS
DELETE
FROM
	[VAT_LIEU_XAY_DUNG]
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THAM_SO_GetByMA_THAM_SO]    Script Date: 09/28/2011 22:58:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THAM_SO_GetByMA_THAM_SO]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THAM_SO_GetByMA_THAM_SO*/
CREATE PROCEDURE [dbo].[sproc_THAM_SO_GetByMA_THAM_SO]
@MA_THAM_SO bigint
AS
SELECT
	--[MA_THAM_SO],
	--[PARENT_ID],
	--[LOAI_THAM_SO],
	--[NOI_DUNG],
	--[SEQUENCE],
	--[GHI_CHU]

*
FROM
	[THAM_SO]
WHERE
	[MA_THAM_SO] = @MA_THAM_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_THUA_DAT_Update]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_THUA_DAT_Update]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
/* Procedure sproc_THUA_DAT_Update*/
CREATE PROCEDURE [dbo].[sproc_THUA_DAT_Update]
	@SO_THUA_DAT NVARCHAR(50),
	@TO_BAN_DO NVARCHAR(50),
	@CHIEU_DAI float,
	@CHIEU_RONG float,
	@TONG_DIEN_TICH float,
	@DIEN_TICH_CONG_NHAN float,
	@DIEN_TICH_KHONG_CONG_NHAN float,
	@DIEN_TICH_SU_DUNG_CHUNG float,
	@LOAI_HINH_DANG_THUA_DAT bigint,
	@HINH_DANG_THUA_DAT NVARCHAR(50),
	@LOAI_DUONG_PHO bigint,
	@LO_GIOI float,
	@SO_MAT_TIEN NVARCHAR(50),
	@LOAI_LAN_DUONG bigint,
	@NOI_DUNG_LAN_DUONG NVARCHAR(50),
	@LOAI_HEM bigint,
	@MA_HO_SO bigint

AS
UPDATE [THUA_DAT]
SET
	[SO_THUA_DAT] = @SO_THUA_DAT,
	[TO_BAN_DO] = @TO_BAN_DO,
	[CHIEU_DAI] = @CHIEU_DAI,
	[CHIEU_RONG] = @CHIEU_RONG,
	[TONG_DIEN_TICH] = @TONG_DIEN_TICH,
	[DIEN_TICH_CONG_NHAN] = @DIEN_TICH_CONG_NHAN,
	[DIEN_TICH_KHONG_CONG_NHAN] = @DIEN_TICH_KHONG_CONG_NHAN,
	[DIEN_TICH_SU_DUNG_CHUNG] = @DIEN_TICH_SU_DUNG_CHUNG,
	[LOAI_HINH_DANG_THUA_DAT] = @LOAI_HINH_DANG_THUA_DAT,
	[HINH_DANG_THUA_DAT] = @HINH_DANG_THUA_DAT,
	[LOAI_DUONG_PHO] = @LOAI_DUONG_PHO,
	[LO_GIOI] = @LO_GIOI,
	[SO_MAT_TIEN] = @SO_MAT_TIEN,
	[LOAI_LAN_DUONG] = @LOAI_LAN_DUONG,
	[NOI_DUNG_LAN_DUONG] = @NOI_DUNG_LAN_DUONG,
	[LOAI_HEM] = @LOAI_HEM
WHERE
	[MA_HO_SO] = @MA_HO_SO
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_Search]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_Search]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_Search]
	@SO_PHIEU NVARCHAR(255),
	@TRANGTHAI CHAR(1), /* Y N T*/
	@TEN_CHU_SU_DUNG NVARCHAR(255),
	@QUANID INT, /* null : -1 */
	@PHUONGID INT, /* null : -1 */
	@DUONG NVARCHAR(50),
	@DOANDUONG NVARCHAR(50),
	@MIN_PRICE FLOAT,
	@MAX_PRICE FLOAT,
	@PageSize INT,
	@PageNo INT -- di tu 1
AS
BEGIN
	
	DECLARE @FromIndex INT
	DECLARE @ToIndex INT
	-- di tu 1
	SET @FromIndex = @PageSize * (@PageNo - 1) + 1
	SET @ToIndex = @PageSize * @PageNo
			   
	SELECT 
		RowNumber ,
		RowTotal,
		MA_HO_SO,
		DUONG,
		DOAN_DUONG,
		TEN_QUAN,
		TEN_PHUONG,
		SO_PHIEU,
		TEN_CHU_SU_DUNG,
		DIA_CHI,
		NGAY_KHOI_TAO,
		NGAY_CAP_NHAT,
		UserName,
		GIA_DAT,
		GIA_GIAO_DICH_TRIEU_DONG,
		GIA_DAT_1,
		GIA_DAT_2,
		GIA_DAT_BINH_QUAN_1,
		GIA_DAT_BINH_QUAN_2,
		ACTIVE_FLAG
	FROM (
		SELECT 
				ROW_NUMBER() OVER(ORDER BY hstd.SO_PHIEU,hstd.NGAY_CAP_NHAT DESC) AS RowNumber,
				COUNT(0) OVER() AS RowTotal,
				hstd.MA_HO_SO,
				hstd.SO_PHIEU,
				dks.DUONG,
				dks.DOAN_DUONG,
				tsqquan.NOI_DUNG AS TEN_QUAN,
				tsphuong.NOI_DUNG AS TEN_PHUONG,
				hstd.TEN_CHU_SU_DUNG,
				hstd.DIA_CHI,
				hstd.NGAY_KHOI_TAO,
				hstd.NGAY_CAP_NHAT,
				au.UserName,
				gdd.GIA_DAT,
				gtd.GIA_GIAO_DICH_TRIEU_DONG,
				hstd.GIA_DAT_1,
				hstd.GIA_DAT_2,
				hstd.GIA_DAT_BINH_QUAN_1,
				hstd.GIA_DAT_BINH_QUAN_2,
				hstd.ACTIVE_FLAG
		FROM   HO_SO_THAM_DINH AS hstd
				INNER JOIN DUONG_KHAO_SAT AS dks
					ON  hstd.MA_DKS = dks.MA_DKS
				INNER JOIN THAM_SO tsqquan
					ON  dks.QUAN = tsqquan.MA_THAM_SO
				INNER JOIN THAM_SO AS tsphuong
					ON  dks.PHUONG = tsphuong.MA_THAM_SO
				INNER JOIN aspnet_Users au
					ON hstd.NGUOI_KHOI_TAO = au.UserId
				INNER JOIN GIA_THAM_DINH gtd
					ON gtd.MA_HO_SO = hstd.MA_HO_SO
				LEFT JOIN GIA_DOAN_DUONG gdd
					ON gdd.MA_DKS = dks.MA_DKS
		WHERE 				
				isnull(hstd.SO_PHIEU,'''')
				LIKE 
				CASE WHEN @SO_PHIEU IS NOT NULL AND @SO_PHIEU <> '''' THEN ''%'' + @SO_PHIEU + ''%'' ELSE isnull(hstd.SO_PHIEU,'''') END
			AND
				isnull(hstd.TEN_CHU_SU_DUNG,'''')
				LIKE 
				CASE WHEN @TEN_CHU_SU_DUNG IS NOT NULL AND @TEN_CHU_SU_DUNG <> '''' THEN ''%'' + @TEN_CHU_SU_DUNG + ''%'' ELSE isnull(hstd.TEN_CHU_SU_DUNG,'''') END
			AND
				tsqquan.MA_THAM_SO
				= 
				CASE WHEN @QUANID <> -1 THEN @QUANID ELSE tsqquan.MA_THAM_SO END
			AND
				tsphuong.MA_THAM_SO
				= 
				CASE WHEN @PHUONGID <> -1 THEN @PHUONGID ELSE tsphuong.MA_THAM_SO END
			AND
				isnull(dks.DUONG,'''')
				LIKE 
				CASE WHEN @DUONG IS NOT NULL AND @DUONG <> '''' THEN ''%'' + @DUONG + ''%'' ELSE isnull(dks.DUONG,'''') END
			AND
				isnull(dks.DOAN_DUONG,'''')
				LIKE 
				CASE WHEN @DOANDUONG IS NOT NULL AND @DOANDUONG <> '''' THEN ''%'' + @DOANDUONG + ''%'' ELSE isnull(dks.DOAN_DUONG,'''') END
			AND
				( gtd.GIA_GIAO_DICH_TRIEU_DONG > @MIN_PRICE AND gtd.GIA_GIAO_DICH_TRIEU_DONG < @MAX_PRICE )			
			AND
				isnull(hstd.ACTIVE_FLAG,'''')
				<>
				CASE WHEN @TRANGTHAI IS NOT NULL AND @TRANGTHAI = '''' THEN ''N'' ELSE '''' END
			AND 
				isnull(hstd.ACTIVE_FLAG,'''')
				=
				CASE WHEN @TRANGTHAI IS NOT NULL AND @TRANGTHAI <> '''' THEN @TRANGTHAI ELSE isnull(hstd.ACTIVE_FLAG,'''') END
			AND (gdd.NGAY_CAP_NHAT
				=
				(SELECT MAX(NGAY_CAP_NHAT) FROM GIA_DOAN_DUONG gdd2 WHERE gdd2.MA_DKS = dks.MA_DKS)
			OR gdd.NGAY_CAP_NHAT IS NULL)
	)  AS rownumber
	WHERE RowNumber BETWEEN @FromIndex AND @ToIndex OR @PageNo = 0
END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_HO_SO_THAM_DINH_SearchByOwner]    Script Date: 09/28/2011 22:58:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_HO_SO_THAM_DINH_SearchByOwner]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[sproc_HO_SO_THAM_DINH_SearchByOwner]
	@USERID UNIQUEIDENTIFIER,
	@TRANGTHAI CHAR(1), /* Y N T*/
	@PageSize INT,
	@PageNo INT -- di tu 1
AS
BEGIN
	
	DECLARE @FromIndex INT
	DECLARE @ToIndex INT
	-- di tu 1
	SET @FromIndex = @PageSize * (@PageNo - 1) + 1
	SET @ToIndex = @PageSize * @PageNo
			   
	SELECT 
		RowNumber ,
		RowTotal,
		MA_HO_SO,
		DUONG,
		DOAN_DUONG,
		TEN_QUAN,
		TEN_PHUONG,
		SO_PHIEU,
		TEN_CHU_SU_DUNG,
		DIA_CHI,
		NGAY_KHOI_TAO,
		NGAY_CAP_NHAT,
		UserName,
		GIA_DAT,
		GIA_GIAO_DICH_TRIEU_DONG,
		GIA_DAT_1,
		GIA_DAT_2,
		GIA_DAT_BINH_QUAN_1,
		GIA_DAT_BINH_QUAN_2,
		ACTIVE_FLAG
	FROM (
		SELECT ROW_NUMBER() OVER(ORDER BY hstd.SO_PHIEU,hstd.NGAY_CAP_NHAT DESC) AS RowNumber,
		       COUNT(0) OVER() AS RowTotal,
		       hstd.MA_HO_SO,
		       hstd.SO_PHIEU,
		       dks.DUONG,
		       dks.DOAN_DUONG,
		       tsqquan.NOI_DUNG AS TEN_QUAN,
		       tsphuong.NOI_DUNG AS TEN_PHUONG,
		       hstd.TEN_CHU_SU_DUNG,
		       hstd.DIA_CHI,
		       hstd.NGAY_KHOI_TAO,
		       hstd.NGAY_CAP_NHAT,
				au.UserName,
		       gdd.GIA_DAT,
		       gtd.GIA_GIAO_DICH_TRIEU_DONG,
		       hstd.GIA_DAT_1,
		       hstd.GIA_DAT_2,
		       hstd.GIA_DAT_BINH_QUAN_1,
		       hstd.GIA_DAT_BINH_QUAN_2,
		       hstd.ACTIVE_FLAG
		FROM   HO_SO_THAM_DINH AS hstd
		       INNER JOIN DUONG_KHAO_SAT AS dks
		            ON hstd.MA_DKS = dks.MA_DKS
		       INNER JOIN THAM_SO tsqquan
		            ON dks.QUAN = tsqquan.MA_THAM_SO
		       INNER JOIN THAM_SO AS tsphuong
		            ON dks.PHUONG = tsphuong.MA_THAM_SO
		        INNER JOIN aspnet_Users au
					ON hstd.NGUOI_KHOI_TAO = au.UserId
				INNER JOIN GIA_THAM_DINH gtd
					ON gtd.MA_HO_SO = hstd.MA_HO_SO
		        LEFT JOIN GIA_DOAN_DUONG gdd
					ON gdd.MA_DKS = dks.MA_DKS
		WHERE 
				hstd.NGUOI_KHOI_TAO
				= 
				CASE WHEN @USERID IS NOT NULL THEN @USERID ELSE hstd.NGUOI_KHOI_TAO END
			AND
				isnull(hstd.ACTIVE_FLAG,'')
				= 
				CASE WHEN @TRANGTHAI IS NOT NULL AND @TRANGTHAI <> '' THEN @TRANGTHAI ELSE isnull(hstd.ACTIVE_FLAG,'Y') END				
			AND hstd.ACTIVE_FLAG <> 'N'
			AND ( gdd.NGAY_CAP_NHAT
				=
				(SELECT MAX(NGAY_CAP_NHAT) FROM GIA_DOAN_DUONG gdd2 WHERE gdd2.MA_DKS = dks.MA_DKS)
				OR gdd.NGAY_CAP_NHAT IS NULL)
	)  AS rownumber
	WHERE RowNumber BETWEEN @FromIndex AND @ToIndex OR @PageNo = 0
END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_searchDuong]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_searchDuong]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N' 
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_searchDuong]
	@QUANID INT,
	@PHUONGID INT
AS
BEGIN
	SELECT
		dks.DUONG
		--dks.NGAY_KHOI_TAO,
		--dks.ACTIVE_FLAG
	FROM
		DUONG_KHAO_SAT dks
	WHERE dks.QUAN = @QUANID AND dks.PHUONG = @PHUONGID
	GROUP BY dks.DUONG
	
END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_DUONG_KHAO_SAT_searchDoanDuong]    Script Date: 09/28/2011 22:58:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_DUONG_KHAO_SAT_searchDoanDuong]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N' 
CREATE PROCEDURE [dbo].[sproc_DUONG_KHAO_SAT_searchDoanDuong]
	@QUANID INT,
	@PHUONGID INT
AS
BEGIN
	SELECT
		dks.DOAN_DUONG
	FROM
		DUONG_KHAO_SAT dks
	WHERE dks.QUAN = @QUANID AND dks.PHUONG = @PHUONGID
	GROUP BY dks.DOAN_DUONG
	
END


/* test */
--EXEC sproc_DUONG_KHAO_SAT_searchDuong 77,101' 
END
GO
/****** Object:  StoredProcedure [dbo].[sproc_VAT_LIEU_XAY_DUNG_Add]    Script Date: 09/28/2011 22:58:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_VAT_LIEU_XAY_DUNG_Add]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sproc_VAT_LIEU_XAY_DUNG_Add]
	@MA_HO_SO bigint
	,@LOAI_KET_CAU_KHUNG bigint
	,@NOI_DUNG_KET_CAU_KHUNG NVARCHAR(50)
	,@LOAI_KET_CAU_MONG bigint
	,@NOI_DUNG_KET_CAU_MONG NVARCHAR(50)
	,@LOAI_KET_CAU_TUONG_NHA bigint
	,@NOI_DUNG_KET_CAU_TUONG_NHA NVARCHAR(50)
	,@LOAI_KET_CAU_NEN bigint
	,@NOI_DUNG_KET_CAU_NEN NVARCHAR(50)
	,@LOAI_KET_CAU_MAI bigint
	,@NOI_DUNG_KET_CAU_MAI NVARCHAR(50)
	,@CHAT_LUONG_MONG float
	,@CHAT_LUONG_SAN float
	,@CHAT_LUONG_TUONG float
	,@CHAT_LUONG_TRAN float
	,@CHAT_LUONG_MAI float
	,@TI_LE_KET_CAU_MONG float
	,@TI_LE_KET_CAU_SAN float
	,@TI_LE_KET_CAU_TUONG float
	,@TI_LE_KET_CAU_TRAN float
	,@TI_LE_KET_CAU_MAI float
	,@SO_PHONG_NGU int
	,@SO_NHA_VE_SINH int
	,@LOAI_TRANG_TRI_NOI_THAT bigint
	,@NOI_DUNG_TRANG_TRI_NOI_THAT NVARCHAR(50)
	,@LOAI_CONG_TRINH_XAY_DUNG bigint
	,@NOI_DUNG_CONG_TRINH_XAY_DUNG NVARCHAR(50)

AS

	INSERT INTO [VAT_LIEU_XAY_DUNG]
	(
		[MA_HO_SO],
		[LOAI_KET_CAU_KHUNG],
		[NOI_DUNG_KET_CAU_KHUNG],
		[LOAI_KET_CAU_MONG],
		[NOI_DUNG_KET_CAU_MONG],
		[LOAI_KET_CAU_TUONG_NHA],
		[NOI_DUNG_KET_CAU_TUONG_NHA],
		[LOAI_KET_CAU_NEN],
		[NOI_DUNG_KET_CAU_NEN],
		[LOAI_KET_CAU_MAI],
		[NOI_DUNG_KET_CAU_MAI],
		[CHAT_LUONG_MONG],
		[CHAT_LUONG_SAN],
		[CHAT_LUONG_TUONG],
		[CHAT_LUONG_TRAN],
		[CHAT_LUONG_MAI],
		[TI_LE_KET_CAU_MONG],
		[TI_LE_KET_CAU_SAN],
		[TI_LE_KET_CAU_TUONG],
		[TI_LE_KET_CAU_TRAN],
		[TI_LE_KET_CAU_MAI],
		[SO_PHONG_NGU],
		[SO_NHA_VE_SINH],
		[LOAI_TRANG_TRI_NOI_THAT],
		[NOI_DUNG_TRANG_TRI_NOI_THAT],
		[LOAI_CONG_TRINH_XAY_DUNG],
		[NOI_DUNG_CONG_TRINH_XAY_DUNG]
	)
	VALUES
	(
		@MA_HO_SO,
		@LOAI_KET_CAU_KHUNG,
		@NOI_DUNG_KET_CAU_KHUNG,
		@LOAI_KET_CAU_MONG,
		@NOI_DUNG_KET_CAU_MONG,
		@LOAI_KET_CAU_TUONG_NHA,
		@NOI_DUNG_KET_CAU_TUONG_NHA,
		@LOAI_KET_CAU_NEN,
		@NOI_DUNG_KET_CAU_NEN,
		@LOAI_KET_CAU_MAI,
		@NOI_DUNG_KET_CAU_MAI,
		@CHAT_LUONG_MONG,
		@CHAT_LUONG_SAN,
		@CHAT_LUONG_TUONG,
		@CHAT_LUONG_TRAN,
		@CHAT_LUONG_MAI,
		@TI_LE_KET_CAU_MONG,
		@TI_LE_KET_CAU_SAN,
		@TI_LE_KET_CAU_TUONG,
		@TI_LE_KET_CAU_TRAN,
		@TI_LE_KET_CAU_MAI,
		@SO_PHONG_NGU,
		@SO_NHA_VE_SINH,
		@LOAI_TRANG_TRI_NOI_THAT,
		@NOI_DUNG_TRANG_TRI_NOI_THAT,
		@LOAI_CONG_TRINH_XAY_DUNG,
		@NOI_DUNG_CONG_TRINH_XAY_DUNG
	)' 
END
GO
